	
define([
	'vue',
	'config',
	'common',
	'../model/upLoadList',
	'../model/downLoadList',
	'moment',
	'../model/router'
	],function(Vue,config,utils,upLoadList,downLoadList,moment,router){
	'use strict';

	var MessageView=new Vue({
		el:'#app',
		data:{
			items:'',
			page: 1,
			totalPage:0,
			pageSize:'',
			active:'',		//选中类型样式
			activeIndex:null,
			isFirstLoad: true
		},
		router:router,
		filters:{
			moment:function(date){
				var Date = moment(date*1000).format('YYYY-MM-DD HH:mm:ss');
				return Date;
			}
		},
		watch:{
			'$route':function(to,from){  
            	//console.log('从'+from.params.id+'到'+to.params.id);
            	var _this=this;
            	_this.items = '';
              	var messageType = (to.params.id === '0') ? null:to.params.id;
              	_this.activeIndex = messageType;

              	utils.setData('messageType',messageType);
            	_this.page = 1;//清空页数
				upLoadList.ajax(utils.getWebUrl('message/list'),{
					pageSize:_this.pageSize,
					page:_this.page,
					messageType:messageType
				},function(res){
					_this.items = res.data.list;
					_this.totalPage = res.data.totalPage;
					_this.page++;

					var ids = [];
					$.each(_this.items,function(k,p){
						if(p.isRead == '0'){
							ids.push(p.id)
						}
					})
					if (ids.length != 0) {
						utils.ajax(utils.getWebUrl('message/read'),{
							ids: ids
						},'POST',function(res){})
					}
				})
       		},
       		items:function(to,from){
       			//console.log(this.items)

       			if(to.length == 0 && this.items !== ''){
       				$('.error404').show();
       			}else{
       				$('.error404').hide();
       			}
       		}
		},
		created:function(){
			utils.userToken();

			var _this=this;
			_this.pageSize = 30;
			var messageType = utils.getData('messageType');

			upLoadList.ajax(utils.getWebUrl('message/list'),{
				pageSize:_this.pageSize,
				page:_this.page,
				messageType:messageType
			},function(res){
				_this.items = res.data.list;
				_this.totalPage = res.data.totalPage;

				var ids = [];
				$.each(_this.items,function(k,p){
					if(p.isRead == '0'){
						ids.push(p.id)
					}
				})
				if (ids.length != 0) {
					utils.ajax(utils.getWebUrl('message/read'),{
						ids: ids
					},'POST',function(res){})
				}
			})
		},
		directives: {
		  scroll: {
		    inserted: function (el, binding){
		      var binding =binding;
		      window.addEventListener('scroll',function() {
		       		var fnc = binding.value;   
   					fnc();
		      })
		    }
		  }
		},
		methods:{
			toRefresh: function(){
				window.location.reload();
			},
			loadMore:function(event){
				var _this = this;
				if ((upLoadList.getScrollTop() + upLoadList.getClientHeight() >= upLoadList.getScrollHeight())&&_this.page<_this.totalPage) {
		      		_this.page = _this.page+1;
		      		var data = {
		      			pageSize:_this.pageSize,
						page:_this.page,
						messageType: _this.messageType
		      		}
		      		
		            upLoadList.ajax(utils.getWebUrl('message/list'),data,function(res){
						_this.items = _this.items.concat(res.data.list);

						var ids = [];
						$.each(_this.items,function(k,p){
							if(p.isRead == '0'){
								ids.push(p.id)
							}
						})
						if (ids.length != 0) {
							utils.ajax(utils.getWebUrl('message/read'),{
								ids: ids
							},'POST',function(res){})
						}
					})
		        }
			},
			backhome:function(){
				utils.setData('messageType','');
				var userTypeId =utils.getData('userTypeId');
				if(userTypeId == 4){
					window.location.href='userInfo.html';
				}else{
					window.location.href='shopInfo.html';
				}
			},

			toDetail:function(event){
				var $target = $(event.currentTarget);
				var id = $target.data('id');
				window.location.href = "MessageDetail.html?messageId="+id;
			},

			Start:function(event){
				var touch = event.targetTouches[0];
				this.start = touch.pageY;
			},

			Move:function(event){
				var slideDown1 = $('#pullDown1');

				var touch = event.targetTouches[0];
				this.end = (this.start - touch.pageY);
				
				//下滑才执行操作 
                if(this.end < -10){
                	
                	slideDown1.text('下拉刷新↓');
	        		slideDown1.show();
	        		slideDown1[0].style.height =  (parseInt("0px")-this.end) + "px";
	        		if(this.end < -48){
	        			/*slideDown1.hide(); 
	        			downLoadList.slideDownStep1(this.end);*/
	        			slideDown1.text('松手开始更新↑')
	        		}else{
	        			slideDown1[0].style.height =  0 + "px";
	        		}
                }else{
                	slideDown1[0].style.height =  0 + "px";
                }
			},

			End:function(event){
				var _this = this;
				var slideDown1 = $('#pullDown1');
				// /console.log(this.end)	
				if(this.end >0 ){  
					slideDown1.hide();
	            }else{ 
	                if(this.end < -48 && document.body.scrollTop == 0) {
	                	var slideDown1 = $('#pullDown1');
	                	var slideDown2 = $('#pullDown2');
						//$('.pullDownLabel').text("下拉刷新...");
						slideDown1.hide();
				        slideDown2.show();

	                	downLoadList.slideDownStep2(); 
		                //刷新成功则 
		                //模拟刷新成功进入第三步

		                //请求数据
		                _this.page = 1;
		                //_this.items = '';
		                $('#pullDown3').hide();	
		                var messageType = utils.getData('messageType');
		                utils.ajax(utils.getWebUrl('message/list'),{
		                	pageSize:_this.pageSize,
							page:_this.page,
							messageType:messageType
		                },
		                'POST',
		                function(res){
							_this.items = res.data.list;
							_this.totalPage = res.data.totalPage;

							var ids = [];
							$.each(_this.items,function(k,p){
								if(p.isRead == '0'){
									ids.push(p.id)
								}
							})
							if (ids.length != 0) {
								utils.ajax(utils.getWebUrl('message/read'),{
									ids: ids
								},'POST',function(res){})
							}
						},null,function(){
							downLoadList.slideDownStep3();
						})
		                
	                }else{
	                	var slideDown1 = $('#pullDown1');
		        		slideDown1.hide();

	                }
	            }
			}
		}
	})

	return MessageView;
});